﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public class DynamiteObject:DestructableObject
{
    protected Vector3 aura = new Vector3(200, 200, 200);
    protected new DynamiteObject.Type thisType;
    protected List<DestructableObject> destructables;
    protected int lastFrame = -1;

    public DynamiteObject(DynamiteObject.Type type, Vector3 placement):base(type, placement)
    {
        thisType = type;
        stayActive = true;
        destructables = new List<DestructableObject>();
        List<CollidableObject> nearby = LevelBase.collidesWith(min-aura, max+aura,this);
        foreach(CollidableObject co in nearby)
        {
            if(co is DestructableObject)
            {
                destructables.Add((DestructableObject)co);
                ((DestructableObject)co).stayInMemory(true);
            }
        }
    }

    public override void destroy(CollidableObject cobj)
    {
        Game.getGame().getSoundManager().playOneSoundEffect(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.SOUND_EFFECT).get("dynamite_explode")).sound, 1.0f);
        base.destroy(cobj);

    }

    public override void update(double elapsed)
    {
        if (currentAnimation == stasis && currentAnimation.isFinished)
        {
            destroy(this);
        }
        if (currentAnimation == destruction && currentAnimation.isFinished)
        {
            cull = true;
            foreach (DestructableObject o in destructables)
            {
                o.stayInMemory(false);
            }
        }
        base.update(elapsed);
        if (destroyed && lastFrame < currentAnimation.getFrameNumber())
        {
            lastFrame = currentAnimation.getFrameNumber();
            Vector2 frameOrigin = ((Frame)currentAnimation.frames[currentAnimation.getFrameNumber()]).origin;
            aura = new Vector3(frameOrigin, frameOrigin.X);
            List<CollidableObject> nearby = LevelBase.collidesWith(origin - aura, origin + aura, this);
            foreach (CollidableObject co in nearby)
            {
                if (co is DestructableObject)
                {
                    ((DestructableObject)co).destroy(this);
                }
            }
        }
    }

    public new class Type : DestructableObject.Type
    {

        public static readonly DynamiteObject.Type TNT = new Type(
            ObjectFactory.TypeEnum.TNT,//type
            new Vector3(26, 36, 0),//origin
            new Vector3(53, 36, 36),//collision
            false,//walkable
            new ArrayList {
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.STATIC_WORLD_OBJECTS).get("dynamiteSpriteSheet"), 
                    new Rectangle(0,0,73,36), 
                    0, 
                    new Vector2(36,36)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.STATIC_WORLD_OBJECTS).get("dynamiteSpriteSheet"), 
                    new Rectangle(73,0,73,36), 
                    0, 
                    new Vector2(36,36)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.STATIC_WORLD_OBJECTS).get("dynamiteSpriteSheet"), 
                    new Rectangle(146,0,73,36), 
                    0, 
                    new Vector2(36,36)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.STATIC_WORLD_OBJECTS).get("dynamiteSpriteSheet"), 
                    new Rectangle(219,0,73,36), 
                    0, 
                    new Vector2(36,36)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.STATIC_WORLD_OBJECTS).get("dynamiteSpriteSheet"), 
                    new Rectangle(292,0,73,36), 
                    0, 
                    new Vector2(36,36))},//stasis frames
            1,//stasisFramerate
            false,//stasisLoopable
            new ArrayList {
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(0,0,505,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(505,0,505,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(1010,0,505,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(1515,0,506,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(0,285,505,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(505,285,505,285), 
                    0, 
                    new Vector2(252,285)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("explosionSpriteSheet"), 
                    new Rectangle(1010,285,505,285), 
                    0, 
                    new Vector2(252,285))},//explosionFramelist
            6);//explosion framerate

        public static readonly DynamiteObject.Type Nuke = new Type(
            ObjectFactory.TypeEnum.Nuke,//type
            new Vector3(35, 114, 0),//origin
            new Vector3(70, 114, 70),//collision
            new ArrayList {
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("nukeMissile"), 
                    new Rectangle(0,0,70,114), 
                    0, 
                    new Vector2(35,114))},//stasis frames
            new ArrayList {
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("nukeExplosion1"), 
                    new Rectangle(0,0,1030,579), 
                    0, 
                    new Vector2(515,400)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("nukeExplosion2"), 
                    new Rectangle(0,0,1136,649), 
                    0, 
                    new Vector2(568,500)),
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.HAZARD).get("nukeExplosion3"), 
                    new Rectangle(0,0,1704,922), 
                    0, 
                    new Vector2(852,750))});//explosionFramelist

        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision,
            ArrayList stasisFrames, ArrayList explosionFrames)
            : base(type, origin, collision, stasisFrames, explosionFrames)
        {
        }

        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision, Boolean walkable,
            ArrayList stasisFrames, int stasisFramerate, Boolean stasisLoopable,
            ArrayList explosionFrames, int explosionFramerate)
            : base(type, origin, collision, walkable, stasisFrames, "stasisName", stasisFramerate, stasisLoopable,
            explosionFrames, "explosionName", explosionFramerate, false)
        {
        }

        public static new DynamiteObject.Type getType(ObjectFactory.TypeEnum type)
        {
            switch (type)
            {
                case ObjectFactory.TypeEnum.TNT: return TNT;
                case ObjectFactory.TypeEnum.Nuke: return Nuke;
            }
            return null;
        }
    }
}
